<?php

function img_text($str, $size = 15, $red = 225, $green = 225, $blue = 225, $font = "coprgtb.ttf", $angle = 0){
	global $logger, $homePath;
	$logger->info('Start=======================================img_text function=======================================Start');
        $logger->info('Memory Usage: ' . memory_get_usage());
	$imgName = md5($str . $size . $red . $green . $blue . $font . $angle);
        $img = glob($homePath . '/images/html/' . $imgName . '.*');
	if($img){
		$logger->info('Image already made returning path to image');
                $logger->info('Memory Usage: ' . memory_get_usage());
		$logger->info('End=======================================img_text function=======================================End');
		return $img[0];
	}
	
	$font = $homePath . '/fonts/' . $font;
	
	$imgsize = imageTTFBBox($size, $angle, $font, $str);
	$image = imageCreateTrueColor(abs($imgsize[2]) + abs($imgsize[0]), abs($imgsize[7]) + abs($imgsize[1]));
	imageSaveAlpha($image, true);
	ImageAlphaBlending($image, false);

	$transparentColor = imagecolorallocatealpha($image, $red, $green, $blue, 127);
	imagefill($image, 0, 0, $transparentColor);

	$textColor = imagecolorallocate($image, $red, $green, $blue);
	$shadowColor = imagecolorallocate($image, 0, 0, 0);
	imagettftext($image, $size, 0, -2, abs($imgsize[5]), $shadowColor, $font, $str);
	imagettftext($image, $size, 0, 0, abs($imgsize[5]+1), $textColor, $font, $str);
	imagepng($image, $homePath . '/images/html/' . $imgName . ".png");
	imagedestroy($image);
	
	$logger->info('Image made, saved to ' . $homePath . '/images/html/' . $imgName . ".png" . ' returning path to image');
        $logger->info('Memory Usage: ' . memory_get_usage());
	$logger->info('End=======================================img_text function=======================================End');
	return $homePath . '/images/html/' . $imgName . ".png";
}

function img_overlay($baseImg, $overlayImg, $path = 'cache/', $dst_x = 0, $dst_y = 0){
	global $logger, $homePath;
        $logger->info('Start=======================================img_text function=======================================Start');
        $logger->info('Memory Usage: ' . memory_get_usage());
	$path .= (substr($path, -1) == '/' ? '' : '/');
	$cache = $homePath . '/images/' . $path;
	$imgName = md5($baseImg . $overlayImg . $dst_x . $dst_y);
	
        $img = glob($cache . $imgName . '.*');
        
	if($img){
            $logger->info('Image already made returning path to image');
            $logger->info('Memory Usage: ' . memory_get_usage());
            $logger->info('End=======================================img_text function=======================================End');
            return $img[0];
        }
	
	
	$image = imagecreatefromstring(file_get_contents($baseImg));
	$imageW = imagesx($image);
	$imageH = imagesy($image);
	$frame = imagecreatefromstring(file_get_contents($overlayImg));
	imagealphablending($frame, true);
	imagesavealpha($frame, true);

	$frameW = imagesx($frame);
	$frameH = imagesy($frame);
	
	imagecopy($image, $frame, $dst_x, $dst_y, 0, 0, $frameW, $frameH);
	
	imagejpeg($image, $cache . $imgName . '.jpg');
	
	imagedestroy($image);
	imagedestroy($frame);
	
        $logger->info('Memory Usage: ' . memory_get_usage());
	$logger->info('End=======================================img_text function=======================================End');
	return $cache . $imgName . '.jpg';

}



?>